/*
  Copyright (C) 2008 Toshiaki Maki <makingx@gmail.com>
  All rights reserved.
  
  Redistribution and use in source and binary forms, with or without modification,
  are permitted provided that the following conditions are met:
  
  1. Redistributions of source code must retain the above copyright notice,
     this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright notice,
     this list of conditions and the following disclaimer in the documentation
     and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products derived
     from this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  OF SUCH DAMAGE.
*/

/* -*- coding:utf-8-unix mode:c++ -*- */
#ifndef _BLACKPINE_H
#define _BLACKPINE_H
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <sstream>
#include <fstream>
#include <exception>
#include <sys/time.h> /* gettimeofday */
#include "BlackPinePlugin.h"

class BlackPine
{
  //typedef std::map<const char*, const std::string*> string_map;
  typedef std::map<const std::string, const std::string*> string_map;
  std::string left_delimiter;
  std::string right_delimiter;
  std::string template_dir;
  std::string compile_dir;
  std::string plugin_dir;

  std::ifstream ifs;  
  string_map variable;
  std::map<std::string, BlackPinePlugin*> plugins;
  string_map to_string_map; /* for BlackPine::toString(const char* fname) */

  double start_time;
  void parse(std::ostream& strm, const std::string& str);
  void expandValue(std::ostream& strm, const std::string& str);
  void expand(std::ostream& strm, std::istream& is);
  void expand(std::ostream& strm, const char* fname);
  void setConfig(std::istringstream& strm);
  
  const std::string& getValue(const char* key) {
    static const std::string EMPTY_VALUE = "";
    string_map::iterator it = variable.find(key);
    if (it != variable.end()) {      
      return *it->second;
    }
    return EMPTY_VALUE;
  }

  const std::string& getValue(const std::string& key) {
    return getValue(key.c_str());
  }

public:
  BlackPine() : left_delimiter("<{"), right_delimiter("}>"), template_dir("./"), compile_dir("./"), plugin_dir("./")
  {    
    start_time = getTime();
  };
  ~BlackPine();
  
  void setLeftDelimiter(const char* ld) 
  {
    left_delimiter = ld;
  }

  /**
   * for SWIG
   * use char* instead of std::string& in public methods
   */

  const char* getLeftDelimiter() const
  {
    return left_delimiter.c_str();
  }

  void setRightDelimiter(const char* rd) 
  {
    right_delimiter = rd;
  }

  const char* getRightDelimiter() const
  {
    return right_delimiter.c_str();
  }

  void setTemplateDir(const char* dir) 
  {
    template_dir = dir;
  }

  const char* getTemplateDir() const
  {
    return template_dir.c_str();
  }

  void setPluginDir(const char* dir) 
  {
    plugin_dir = dir;
  }

  const char* getPluginDir() const
  {
    return plugin_dir.c_str();
  }

  void assign(const char* key, const char* value)
  {
    string_map::iterator it = variable.find(key);
    if (it != variable.end()) {      
      variable.erase(key);
      delete it->second;
    }
    std::string* v = new std::string(value);
    variable.insert(make_pair(key, v));
  };

  void display(const char* fname) {
    expand(std::cout, fname);
  };

  const char* toString(const char* fname) 
  { 
    string_map::iterator it = to_string_map.find(fname);
    if (it != to_string_map.end()) {
      return it->second->c_str();
    }

    std::ostringstream oss;
    expand(oss, fname);
    std::string* str = new std::string(oss.str());
    to_string_map.insert(make_pair(fname, str));
    return str->c_str();
  };

  void loadConfig(const char* fname);
      
  static double getTime()
  {
    struct timeval tv;
    gettimeofday(&tv, NULL);
    return tv.tv_sec + (double)tv.tv_usec*1e-6;
  }
};
#endif
